name: Build and Publish Docs to GitHub Pages

on:
  release:
    types:
      - created
  # Allow running this workflow manually from the Actions tab
  workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
  contents: read
  pages: write
  id-token: write
  packages: write

jobs:
  deploy:
    runs-on: ubuntu-latest

    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - uses: actions/checkout@v4

      - name: Set up JDK 21
        uses: actions/setup-java@v3
        with:
          java-version: '21'
          distribution: 'temurin'
          cache: 'maven'

      - name: Generate Javadoc
        run: mvn javadoc:aggregate -DskipTests

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '20.x'

      - run: cd docs && npm ci
      - run: cd docs && npm run build
      - run: cp -R target/site/apidocs docs/build/

      - name: Setup Pages
        uses: actions/configure-pages@v3
      - name: Upload artifact
        uses: actions/upload-pages-artifact@v2
        with:
          # Upload entire repository
          path: './docs/build/.'
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v2